import { defineStore } from "pinia";
import router from '@/router';
import request from "@/plugins/request";

export const useUploadApiStore = defineStore('UploadApiStore', () => {
    const apiInfoForm = ref({
        name:'',
        url:'',
        description:'',
        method:'',
        requestHeader:'',
        createTime:'',
        status:1,
        userId:1
    })

    // 上传
    async function onSubmit(){
        // 把布尔值修改成实数值 添加上传用户信息
        apiInfoForm.value.status=apiInfoForm.value.status?1:0;
        const user = JSON.parse(localStorage.getItem('userInfo') || '')
        apiInfoForm.value.userId = user.id
        // 发送后端
        const {data} = await request.post('/apiInfo/upload',apiInfoForm.value)
        console.log(data)
        if(data.code==200){
            // 成功
            swal("Success", "上传成功", "success");
            router.replace('/home');
        }else{
            // 失败
            swal("Error", "上传失败", "error");
            
        }
    }

    // 重置
    function resetForm(){
        location.reload()
    }

    return {
        apiInfoForm,
        onSubmit,
        resetForm
    }
})